/*
 * Created by JFormDesigner on Tue Jul 02 00:38:58 CST 2024
 */

package scct.hys.ui;

import scct.hys.dao.HallDao;
import scct.hys.dao.UserDao;
import scct.hys.model.Hall;
import scct.hys.model.User;

import java.awt.*;
import java.awt.event.*;
import java.util.List;
import java.util.Objects;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

/**
 * @author 26984
 */
public class PanelHall extends JPanel {
    FormMain main;
    DefaultTableModel model;
    DefaultTableModel Searchmodel=null;
    int id;

    public PanelHall(FormMain main) {

        initComponents();
        this.main = main;
        initView();

    }

    private void initView() {
        model = (DefaultTableModel) tabHall.getModel();
        model.addColumn("id");
        model.addColumn("电影名称");
        model.addColumn("影厅类型");
        model.addColumn("影厅尺寸");
        model.addColumn("总人数");
        getTable();
//        HallDao hallDao=new HallDao();
//        List<Hall> allhall=hallDao.getAllHalls();
//        for (Hall hall:allhall){
//            cboHall.addItem(hall.getMovieid());
//
//        }
    }

    private void getTable() {
        model.setRowCount(0);
//        HallDao hallDao=new HallDao();
        List<Hall> allHalls = HallDao.getAllHalls();
        for (Hall hall : allHalls) {
            model.addRow(new Object[]{hall.getId(), hall.getMovieid(), hall.getScreentype(), hall.getScreensize(), hall.getTotal()});
        }
        System.out.println(allHalls);

    }

    private void btnAdd(ActionEvent e) {
        // TODO add your code here
        DlgHallAdd dlgHallAdd = new DlgHallAdd(main);
        dlgHallAdd.setModal(true);
        dlgHallAdd.setVisible(true);
        getTable();

    }

    private void btnUpdate(ActionEvent e) {
        // TODO add your code here
        if (tabHall.getSelectedRowCount() == 0) {
            JOptionPane.showMessageDialog(this, "请选择修改对象");
            return;
        }
        int index = tabHall.getSelectedRow();
        int id = Integer.parseInt(model.getValueAt(index, 0).toString());
        DlgHallUpdate dlgHallUpdate = new DlgHallUpdate(main, id);

        dlgHallUpdate.setModal(true);
        dlgHallUpdate.setVisible(true);
        getTable();
    }

    private void btnDel(ActionEvent e) {
        // TODO add your code here
        if (tabHall.getSelectedRowCount() == 0) {
            JOptionPane.showMessageDialog(this, "删除成功");
            return;
        }
        int index = tabHall.getSelectedRow();
        int id = Integer.parseInt(model.getValueAt(index, 0).toString());
        HallDao hallDao = new HallDao();
        hallDao.delHall(id);
        getTable();

    }

    private void btnSearch(ActionEvent e) {
        // TODO add your code here
        tabHall.setModel(model);
        Searchmodel = new DefaultTableModel();
        Searchmodel.addColumn("id");
        Searchmodel.addColumn("电影名称");
        Searchmodel.addColumn("影厅类型");
        Searchmodel.addColumn("影厅尺寸");
        Searchmodel.addColumn("总人数");
        Searchmodel.setRowCount(0);
        String searchkey = txtSearchkey.getText();
        System.out.println(searchkey);
        String searchlist = (String) cboHall.getSelectedItem();
        System.out.println(searchlist);
        int row = tabHall.getRowCount();
        System.out.println("row:" + row);
        int column = tabHall.getColumnCount();
        System.out.println("column" + column);
        for (int i = 0; i < row; i++) {
            if (tabHall.getRowCount() < row) {
            } else {

                for (int j = 0; j < column; j++) {
                    if (Objects.equals(searchlist, tabHall.getColumnName(j))) {
                        System.out.println(0002);
                        System.out.println("单元格的值:" + tabHall.getValueAt(i, j));
                        String cellvalue = tabHall.getValueAt(i, j).toString().toLowerCase();
                        System.out.println(cellvalue.contains(searchkey));
                        if (cellvalue.contains(searchkey)) {

                            Object[] rowDate = new Object[tabHall.getColumnCount()];
                            for (int k = 0; k < tabHall.getColumnCount(); k++) {
                                rowDate[k] = tabHall.getValueAt(i, k);
                                System.out.println(rowDate[k]);
                            }
                            Searchmodel.addRow(rowDate);
                            tabHall.setRowSelectionAllowed(true);
                            break;
                        }
                    }

                }
            }

        }
        tabHall.setModel(Searchmodel);
    }


    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - 韩月生
        panel1 = new JPanel();
        btnAdd = new JButton();
        btnDel = new JButton();
        btnUpdate = new JButton();
        cboHall = new JComboBox<>();
        txtSearchkey = new JTextField();
        btnSearch = new JButton();
        scrollPane1 = new JScrollPane();
        tabHall = new JTable();

        //======== this ========
        setBorder (new javax. swing. border. CompoundBorder( new javax .swing .border .TitledBorder (new javax. swing.
        border. EmptyBorder( 0, 0, 0, 0) , "JF\u006frm\u0044es\u0069gn\u0065r \u0045va\u006cua\u0074io\u006e", javax. swing. border. TitledBorder. CENTER
        , javax. swing. border. TitledBorder. BOTTOM, new java .awt .Font ("D\u0069al\u006fg" ,java .awt .Font
        .BOLD ,12 ), java. awt. Color. red) , getBorder( )) );  addPropertyChangeListener (
        new java. beans. PropertyChangeListener( ){ @Override public void propertyChange (java .beans .PropertyChangeEvent e) {if ("\u0062or\u0064er"
        .equals (e .getPropertyName () )) throw new RuntimeException( ); }} );
        setLayout(new BorderLayout());

        //======== panel1 ========
        {
            panel1.setLayout(new FlowLayout(FlowLayout.LEFT, 25, 5));

            //---- btnAdd ----
            btnAdd.setText("\u65b0\u589e\u5f71\u5385");
            btnAdd.addActionListener(e -> btnAdd(e));
            panel1.add(btnAdd);

            //---- btnDel ----
            btnDel.setText("\u5220\u9664");
            btnDel.addActionListener(e -> btnDel(e));
            panel1.add(btnDel);

            //---- btnUpdate ----
            btnUpdate.setText("\u4fee\u6539");
            btnUpdate.addActionListener(e -> btnUpdate(e));
            panel1.add(btnUpdate);

            //---- cboHall ----
            cboHall.setModel(new DefaultComboBoxModel<>(new String[] {
                "id",
                "\u7535\u5f71\u540d\u79f0",
                "\u5f71\u5385\u7c7b\u578b",
                "\u5f71\u5385\u5c3a\u5bf8",
                "\u603b\u4eba\u6570"
            }));
            panel1.add(cboHall);

            //---- txtSearchkey ----
            txtSearchkey.setColumns(7);
            panel1.add(txtSearchkey);

            //---- btnSearch ----
            btnSearch.setText("text");
            panel1.add(btnSearch);
        }
        add(panel1, BorderLayout.NORTH);

        //======== scrollPane1 ========
        {
            scrollPane1.setViewportView(tabHall);
        }
        add(scrollPane1, BorderLayout.CENTER);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - 韩月生
    private JPanel panel1;
    private JButton btnAdd;
    private JButton btnDel;
    private JButton btnUpdate;
    private JComboBox<String> cboHall;
    private JTextField txtSearchkey;
    private JButton btnSearch;
    private JScrollPane scrollPane1;
    private JTable tabHall;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
